﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// DataEdit.xaml 的交互逻辑
    /// </summary>
    public partial class DataEdit : Window
    {
        public DataEdit()
        {
            InitializeComponent();
        }
        SQLiteHelper sqlite = new SQLiteHelper();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt=sqlite.getData("select * from type where lang='"+lang+"'");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content =dt.Rows[i]["name"].ToString();
                item.Tag = dt.Rows[i]["id"].ToString();
                combo0.Items.Add(item);
            }

            getDefault(typestr);
            setlang();

        }

        private void setlang()
        {
            
            if (lang.Equals("ug"))
            {
                this.FlowDirection = FlowDirection.RightToLeft;
                this.Title = "ئۇچۇر تەھرىرلەش";
                HintAssist.SetHint(combo0, "تۈرى");
                HintAssist.SetHint(txt_name, "قىممىتى");
                btn_del.Content = "ئۆچۈرۈش";
                btn_ok.Content = "ساقلاش";
                col_name.Header = "قىممىتى";
            }
            else
            {
                this.FlowDirection = FlowDirection.LeftToRight;
                this.Title = "数据编辑";
                HintAssist.SetHint(combo0, "类型");
                HintAssist.SetHint(txt_name, "数据值");
                btn_del.Content = "删除";
                btn_ok.Content = "保存";
                col_name.Header = "数据值";
            }
        }

        public string typestr { get; set; }
        public string lang { get; set; }
        private void Button_Click  (object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name=btn.Name;
            switch (name)
            {
                case "btn_ok":

                    if (combo0.SelectedItem == null)
                    {
                        return;
                    }
                    if (string.IsNullOrEmpty(txt_name.Text))
                    {
                        return;
                    }
                    ComboBoxItem item=(ComboBoxItem)combo0.SelectedItem;
                    string type= item.Tag.ToString();
                    string item_name = txt_name.Text;
                    insertData(item_name, type);
                    getDefault(type);
                    txt_name.Text = "";
                    break;
                case "btn_del":
                     

                    break;
            }
        }

        private void getDefault(string type)
        {
            DataTable dt=sqlite.getData("select * from config where type='" + type + "' and lang='" + lang + "'");
            grid1.ItemsSource = dt.DefaultView;

        }
        private void insertData(string name,string type)
        {
            DataTable dt=sqlite.getData("select * from config where name='" + name + "' and lang='"+lang+"' and type=" + type);
            if (dt.Rows.Count == 0)
            {
                sqlite.insert("insert into config (name,lang,type) values ('"+name+"','"+lang+"','"+type+"')");
            }

        }

    }
}
